package com.buyantech.smweb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;
import javacommon.util.DateTimeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.model.Alliance;
import com.buyantech.smweb.model.Messages;
import com.buyantech.smweb.model.Suggestion;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.MessagesManager;
import com.buyantech.smweb.service.SuggestionManager;
import com.buyantech.smweb.vo.query.MessagesQuery;
import com.buyantech.smweb.vo.query.SuggestionQuery;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

/**
 * 消息盒子
 * @version 1.0
 * @date 2012-05-10
 */

@Controller
public class MessagesController extends BaseSpringController {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = " crdate desc ";
	private MessagesManager messagesManager;
	private final String LIST_ACTION = "redirect:/pages/User/list.do";

	public void setMessagesManager(MessagesManager messagesManager) {
		this.messagesManager = messagesManager;
	}


	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 
	 * 执行搜索 
	 **/
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,MessagesQuery query) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex"))+1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile=request.getParameter("sortField");
		if(sortfile!=null&&!sortfile.equals(""))
		  query.setSortColumns(request.getParameter("sortField")+" "+request.getParameter("sortOrder"));
		Page page = this.messagesManager.searchPage(query);
		List<Messages> list=page.getResult();
		if(list!=null){
			for(Messages messages:list){
				if(StringUtils.isNotEmpty(messages.getCrdate())){
					messages.setCrdate(messages.getCrdate().substring(2));
				}
			}
		}
		modelMap.put("data", page.getResult());
		modelMap.put("total",page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}	
	
	/**
	 * 登出
	 **/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		String id=request.getParameter("id");
		if(id.length()>0){
			id=id.substring(0,id.length()-1);
		}
		try {
			messagesManager.remove("DELETE FROM  Messages WHERE id in("+id+")");
			ajaxPack("{state:true}",request,response);
		} catch (RuntimeException e) {
			ajaxPack("{state:false}",request,response);
		}
		return null;
	}
	
	
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,Messages messages) {
		ModelAndView modelView = new ModelAndView();
		String json=request.getParameter("submitData");
		Gson gson = new Gson();
		Map<String,String> map = new HashMap<String,String>();
		try {
			map = gson.fromJson(json,Map.class);
			messages.setCotent(map.get("remark"));
			messages.setCrdate(DateTimeUtil.getDateTimeString());
			messagesManager.save(messages);
			ajaxPack("{state:true}",request,response);
		} catch (Exception e) {
			ajaxPack("{state:false}",request,response);
		}
		return null;
	}
	 
	
}
